package com.hkws.hkws.service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hkws.hkws.API.DoorFunctionApi;
import com.hkws.hkws.mapper.DoorevenMapper;
import com.hkws.hkws.pojo.Dooreven;
import com.hkws.hkws.pojo.EventsRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;


/**
 * 查询门禁点事件
 */

@Component
public class QueryDoorEeven {
    @Autowired
    private DoorevenMapper doorevenMapper;
    private Object o;

 //@Scheduled(initialDelay = 15, fixedRate = 10)
    public void execute() {
        DoorFunctionApi doorFunctionApi = new DoorFunctionApi();
        EventsRequest eventsRequest = new EventsRequest();
     Dooreven dooreven = new Dooreven();
     eventsRequest.setPageNo(1);
        eventsRequest.setPageSize(1000);
        /**
         * 查询门禁点事件
         */
        String s = doorFunctionApi.events(eventsRequest);
        JSONObject jsonObject = JSONObject.parseObject(s);
        System.out.println(s);
        JSONArray list = (JSONArray) ((JSONObject) jsonObject.get("data")).get("list");
        System.out.println("list"+list);
        for (int i = 0; i < list.size(); i++) {
         JSONObject json1 = (JSONObject) list.get(i);
         System.out.println("json"+"第几"+i+json1);
         Field[] fields = dooreven.getClass().getDeclaredFields();
         for (Field field : fields) {
             // System.out.println(field.getName());
             Iterator<String> iterator = json1.keySet().iterator();
             while (iterator.hasNext()) {
                 String name = iterator.next();
                 if (field.getName().equals(name)) {
                     Object o = json1.get(name);
                     if (field.getType().toString().equals("class java.lang.String")) {
                         String o1;
                         if(o==null)
                         {
                             o1="";

                         }else
                         {
                             o1 = o.toString();
                         }
                         try {
                             field.setAccessible(true);
                             field.set(dooreven, o1);
                         } catch (IllegalAccessException e) {
                             e.printStackTrace();
                         }
                     }else if(field.getType().toString().equals("class java.lang.Integer"))
                     {
                         Integer o1=null;
                         o1=Integer.valueOf(o.toString());
                         try {
                             field.setAccessible(true);
                             field.set(dooreven, o1);
                         } catch (IllegalAccessException e) {
                             e.printStackTrace();
                         }
                     }
                     else {
                         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
                         Date ts = null;
                         try {
                             ts = sdf.parse((String) o);
                         } catch (ParseException e) {
                             e.printStackTrace();
                         }
                         System.out.println(o);
                         try {
                             field.setAccessible(true);
                             field.set(dooreven, ts);
                         } catch (IllegalAccessException e) {
                             e.printStackTrace();
                         }
                     }

                 }

             }

         }
         System.out.println(dooreven);
         doorevenMapper.insert(dooreven);
     }
    }
}